package com.jht.bletool2java.activity.setting.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.R;

import com.jht.bletool2java.activity.setting.bean.SettingItem;

import java.util.List;


public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.SettingLayoutItem>{
    private static final String TAG = "SettingAdapter";

    private final Context mContext;
    private List<SettingItem> settingItemList;


    public SettingAdapter(Context mContext, List<SettingItem> settingItemList) {
        if (mContext == null || settingItemList == null) throw new NullPointerException("mContext == null || settingItemList == null");
        this.mContext = mContext;
        this.settingItemList = settingItemList;
    }

    @NonNull
    @Override
    public SettingLayoutItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(this.mContext).inflate(R.layout.item_setting, parent, false);
        return new SettingLayoutItem(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull SettingLayoutItem holder, int position) {
        holder.mIvItemIcon.setImageResource(settingItemList.get(position).getImgId());
        holder.mTvSettingTitle.setText(settingItemList.get(position).getTitle());
        holder.mTvSettingDescription.setText(settingItemList.get(position).getDescription());
        holder.mRlSettingItemParent.setOnClickListener(settingItemList.get(position).getOnClickListener());
    }

    @Override
    public int getItemCount() {
        return settingItemList.size();
    }


    public static class SettingLayoutItem extends RecyclerView.ViewHolder {
        TextView mTvSettingTitle;
        TextView mTvSettingDescription;
        ImageView mIvItemIcon;
        RelativeLayout mRlSettingItemParent;

        public SettingLayoutItem(@NonNull View convertView) {
            super(convertView);
            mTvSettingTitle = convertView.findViewById(R.id.tv_setting_title);
            mTvSettingDescription = convertView.findViewById(R.id.tv_setting_description);
            mIvItemIcon = convertView.findViewById(R.id.iv_item_icon);
            mRlSettingItemParent = convertView.findViewById(R.id.rl_setting_item_parent);
        }


    }

    public void changeScanTimeText(int time){
        Resources resources = BLEApplication.getBLEApplicationContext().getResources();
        String text = resources.getString(R.string.scan_description)
                + time + resources.getString(R.string.second);
        SettingItem item = settingItemList.get(0);
        item.setDescription(text);
        notifyItemChanged(0);
    }

}
